home *** CD-ROM | disk | FTP | other *** search
/ 1,000+ Great Games / 1_1000 Games.iso / DOSGAMES / BOGGLE.ZIP / SOURCE.ZIP / RANDOM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-03  |  2.2 KB  |  58 lines

  1. /*****************************************************************************
  2. * Program:  RANDOM.CPP
  3. * Purpose:  generates random values so random squares can be selected and
  4. *           a random face on that random square.
  5. *****************************************************************************/
  6. #include "random.hpp"
  7.  
  8.  
  9. /*****************************************************************************
  10. * Function: perm
  11. * Parms:    none
  12. * Purpose:  generates an array representing a random order of squares
  13. * Returns:  nothing
  14. *****************************************************************************/
  15. void Random::perm()
  16. {
  17.    /******************************************************
  18.    ** This function will populate the cube_array with
  19.    ** 16 random integers which represent a random cube
  20.    ** order for the game.
  21.    ******************************************************/
  22.    time_t t;
  23.    int rand_cube, temp_val, i = 0;
  24.  
  25.    /******************************************************
  26.    ** Initialize the array from 1-16.
  27.    ******************************************************/
  28.    for (i = 0; i < 16; i++)
  29.       cube_array[i] = i + 1;
  30.  
  31.    srand( (unsigned) time(&t) );             // grab current time
  32.    for (int perm = 16; perm > 0; perm--)
  33.    {
  34.       rand_cube = (rand() % perm);           // generate random number
  35.  
  36.       temp_val = cube_array[perm-1];         // save the value of last
  37.                                              //  element.
  38.       /******************************************************
  39.       ** This will swap the last index in the array with
  40.       ** the randomly selected index.
  41.       ******************************************************/
  42.       cube_array[perm-1] = cube_array[rand_cube];
  43.       cube_array[rand_cube] = temp_val;
  44.    }
  45. }
  46.  
  47. /*****************************************************************************
  48. * Function: getValue
  49. * Parms:    k - the array element you wish to query
  50. * Purpose:  returns the value from the cube array
  51. * Returns:  the array element you wish to query
  52. *****************************************************************************/
  53. int  Random::getValue(int k)
  54. {
  55.    return(cube_array[k] - 1);
  56. }
  57.  
  58.